package com.zenchn.service.impl;


import com.zenchn.dao.TSysRoleAuthorityMapperMapper;
import com.zenchn.dao.TSysRoleMapper;
import com.zenchn.dynamic.TSysRoleDynamicMapper;
import com.zenchn.enums.CommonStatusEnum;
import com.zenchn.generic.GenericDao;
import com.zenchn.generic.GenericServiceImpl;
import com.zenchn.model.TSysRole;
import com.zenchn.model.TSysRoleAuthorityMapper;
import com.zenchn.model.TSysRoleAuthorityMapperExample;
import com.zenchn.model.TSysRoleExample;
import com.zenchn.service.RoleService;
import com.zenchn.utils.CommonUtils;
import javax.annotation.Resource;

import org.mybatis.dynamic.sql.render.RenderingStrategies;
import org.mybatis.dynamic.sql.select.render.SelectStatementProvider;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

import static com.zenchn.dynamic.TSysAccountRoleMapperDynamicSqlSupport.*;
import static org.mybatis.dynamic.sql.SqlBuilder.*;
import static com.zenchn.dynamic.TSysRoleDynamicSqlSupport.*;
/**
 * 角色Service实现类
 *
 * @author dingxinfa
 * @date 2020-04-16
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class RoleServiceImpl extends GenericServiceImpl<TSysRole, String, TSysRoleExample> implements RoleService {

    @Resource
    private TSysRoleMapper roleMapper;

	@Resource
	private TSysRoleDynamicMapper roleDynamicMapper;

    @Resource
    private TSysRoleAuthorityMapperMapper roleAuthorityMapperMapper;
    
	@Override
	public GenericDao<TSysRole, String, TSysRoleExample> getDao() {
		return roleMapper;
	}




	@Override
	public int disableRole(String roleId) {
		
		TSysRole role=new TSysRole();
		role.setRoleId(roleId);
		role.setStatus(CommonStatusEnum.DISABLE.getCode());
		roleMapper.updateByPrimaryKeySelective(role);
		
		return 1;
	}

	@Override
	public String saveRole(TSysRole role, String[] auths) {
		role.setCreateTime(LocalDateTime.now());
		if(role.getRoleId() != null && !"".equals(role.getRoleId().trim())){
			
			TSysRoleAuthorityMapperExample example = new TSysRoleAuthorityMapperExample();
			example.createCriteria().andRoleIdEqualTo(role.getRoleId());
			roleAuthorityMapperMapper.deleteByExample(example);

			roleMapper.updateByPrimaryKeySelective(role);
		}else{
			role.setRoleId(CommonUtils.randomUUID());
			role.setStatus(CommonStatusEnum.ENABLE.getCode());
			roleMapper.insertSelective(role);
		}
		
		if(auths != null  && auths.length>0){
			for (String aid : auths) {
				TSysRoleAuthorityMapper roleAuthority = new TSysRoleAuthorityMapper();
				roleAuthority.setRoleId(role.getRoleId());
				roleAuthority.setAuthId(aid);
				roleAuthorityMapperMapper.insert(roleAuthority);
			}
		}
		
		return role.getRoleId();
	}

	@Override
	public void deleteRole(List<String> roleIdList) {

		TSysRoleExample roleExample=new TSysRoleExample();
		roleExample.createCriteria().andRoleIdIn(roleIdList).andIsDefaultEqualTo(false);

		TSysRole role=new TSysRole();
		role.setStatus(CommonStatusEnum.DELETE.getCode());
		roleMapper.updateByExampleSelective(role,roleExample);

	}

	@Override
	public List<TSysRole> selectRoleByAccountId(String accountId) {
		SelectStatementProvider selectStatement = select(TSysRole.allColumns())
				.from(TSysRole)
				.leftJoin(TSysAccountRoleMapper).on(TSysAccountRoleMapper.roleId, equalTo(TSysRole.roleId))
				.where(TSysAccountRoleMapper.accountId, isEqualTo(accountId))
				.build()
				.render(RenderingStrategies.MYBATIS3);
		return roleDynamicMapper.selectMany(selectStatement);
	}


}
